package intermediate_representation;

public class AssignRP extends InterRP {
    private String result;
    private String source;

    public AssignRP(String result, String source) {
        this.result = result;
        this.source = source;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getResult() {
        return result;
    }

    public String getSource() {
        return source;
    }

    public int getSourcedim() {
        int dim = 0;
        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == '[') {
                dim++;
            }
        }
        return dim;
    }

    public boolean sourceIsReg() {
        return source.charAt(0) == '$';
    }

    public String getSourceName() {
        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == '[') {
                return source.substring(0, i);
            }
        }
        return source;
    }

    public String getSourceIndex() {
        int begin = 0;
        int end = 0;
        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == '[') {
                begin = i + 1;
            } else if (source.charAt(i) == ']') {
                end = i;
            }
        }
        if (begin == 0 && end == 0) {
            return null;
        }
        return source.substring(begin, end);
    }

    public int getResultdim() {
        int dim = 0;
        for (int i = 0; i < result.length(); i++) {
            if (result.charAt(i) == '[') {
                dim++;
            }
        }
        return dim;
    }

    public boolean resultIsReg() {
        return source.charAt(0) == '$';
    }

    public String getResultName() {
        for (int i = 0; i < result.length(); i++) {
            if (result.charAt(i) == '[') {
                return result.substring(0, i);
            }
        }
        return result;
    }

    public String getResultIndex() {
        int begin = 0;
        int end = 0;
        for (int i = 0; i < result.length(); i++) {
            if (result.charAt(i) == '[') {
                begin = i + 1;
            } else if (result.charAt(i) == ']') {
                end = i;
            }
        }
        if (begin == 0 && end == 0) {
            return null;
        }
        return result.substring(begin, end);
    }

    public String toString() {
        return result + " = " + source;
    }
}
